package cn.xiaohuodui.service;

import cn.xiaohuodui.dao.SkuAttributeMapper;
import cn.xiaohuodui.dao.SkuMapper;
import cn.xiaohuodui.form.back.good.BackGoodCreateForm;
import cn.xiaohuodui.form.back.good.BackGoodDo;
import cn.xiaohuodui.form.back.good.BackGoodQueryForm;
import cn.xiaohuodui.form.back.sku.BackSkuAttributeCreateForm;
import cn.xiaohuodui.form.back.sku.BackSkuCreateForm;
import cn.xiaohuodui.form.back.sku.BackSkuDo;
import cn.xiaohuodui.form.back.sku.BackSkuQueryForm;
import cn.xiaohuodui.model.Good;
import cn.xiaohuodui.model.GoodWithBLOBs;
import cn.xiaohuodui.model.Sku;
import cn.xiaohuodui.model.SkuAttribute;
import cn.xiaohuodui.vo.Sku.SkuVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by TR on 2018/4/3.
 */
@Service
public class SkuService {

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SkuAttributeMapper skuAttributeMapper;


    public SkuVo getBySkuId(Integer id){
        Sku sku = skuMapper.selectByPrimaryKey(id);
        List<SkuAttribute> bySkuId = skuAttributeMapper.getBySkuId(id);
        SkuVo skuVo = new SkuVo();
        skuVo.setId(id);
        skuVo.setName(sku.getName());
        skuVo.setSkuAttributes(bySkuId);
        return skuVo;
    }

















    // ------------------------------------------------> 以下为后台代码

    /**
     * 新建Sku
     *
     * @param backSkuCreateForm
     * @return
     */
    public Sku createSku(BackSkuCreateForm backSkuCreateForm,BackSkuAttributeCreateForm backSkuAttributeCreateForm) {
        Sku sku = new Sku();
        BeanUtils.copyProperties(backSkuCreateForm, sku);
        sku.setCreated(System.currentTimeMillis());
        sku.setUpdated(sku.getCreated());
        sku.setDeleted(Byte.valueOf("0"));
        skuMapper.insert(sku);

        SkuAttribute  skuAttribute = new SkuAttribute();
        BeanUtils.copyProperties(backSkuAttributeCreateForm, skuAttribute);
        skuAttribute.setSkuId(sku.getId());
        skuAttribute.setDeleted(0);
        skuAttribute.setAttribute("111");
        skuAttribute.setCreated(sku.getCreated());
        skuAttribute.setUpdated(sku.getCreated());
        skuAttributeMapper.insert(skuAttribute);

        return sku;
    }

    /**
     * 查询 sku数量
     *
     * @param form
     * @return
     */
    public int skuBackCount(BackSkuQueryForm form) {
        Map qm = new HashMap();
        qm.put("name", form.getName());
        return skuMapper.skuBackCount(qm);
    }

    /**
     * 查询 sku
     *
     * @param form
     * @return
     */
    public List<BackSkuDo> skuBackQuery(BackSkuQueryForm form) {
        Map qm = new HashMap();
        qm.put("limit", form.getLimit());
        qm.put("offset", form.getOffset());

        qm.put("name", form.getName());
        List<Sku> goodList = skuMapper.skuBackQuery(qm);
        if (CollectionUtils.isEmpty(goodList)) {
            return new ArrayList<>();
        }
        return goodList.stream().map(eachListDo -> {
            BackSkuDo skuDo = new BackSkuDo();
            BeanUtils.copyProperties(eachListDo, skuDo);
//            ActivityPublish activity = activityPublishMapper.selectByPrimaryKey(eachListDo.getActivityId());
//            if (activity != null){
//                goodDo.setTitle(activity.getTitle());
//                ActivityGoods good = activityGoodsMapper.selectByPrimaryKey(activity.getActivityGoodId());
//                if (good != null) {
//                    CropType cropType = cropTypeMapper.selectByPrimaryKey(good.getCropTypeId());
//                    goodDo.setCropTypeName(cropType.getTypeName());
//                    goodDo.setCropName(good.getGoodsName());
////                    activityOrderDo.setWeight(good.getWeight());
////                    activityVo.setAmount(good.getAmount());
////                    activityVo.setUsed(good.getUsed());
//                    goodDo.setYear(good.getYear());
////                    activityVo.setOrigin(good.getOrigin());
////                    activityVo.setCropIntro(cropType.getIntro());
//                }
//            }

            return skuDo;

        }).collect(Collectors.toList());
    }



}
